const Database = require('better-sqlite3');
const path = require('path');

const DB_PATH = path.join(process.cwd(), 'data', 'delivery-management.db');

function checkTables() {
  const db = new Database(DB_PATH);
  
  try {
    // 检查所有表
    const tablesStmt = db.prepare("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name");
    const tables = tablesStmt.all();
    
    console.log('数据库中的所有表:');
    tables.forEach(table => {
      console.log(`- ${table.name}`);
    });
    
    // 检查PBS相关的表
    const pbsTables = tables.filter(table => table.name.toLowerCase().includes('pbs'));
    console.log('\nPBS相关的表:');
    pbsTables.forEach(table => {
      console.log(`- ${table.name}`);
      
      // 显示表结构
      const schemaStmt = db.prepare(`PRAGMA table_info(${table.name})`);
      const schema = schemaStmt.all();
      console.log('  字段:');
      schema.forEach(column => {
        console.log(`    ${column.name} (${column.type})`);
      });
      
      // 显示记录数
      const countStmt = db.prepare(`SELECT COUNT(*) as count FROM ${table.name}`);
      const count = countStmt.get();
      console.log(`  记录数: ${count.count}`);
      console.log('');
    });
    
  } catch (error) {
    console.error('检查数据库表失败:', error);
  } finally {
    db.close();
  }
}

if (require.main === module) {
  checkTables();
}